package com.monas.client.dialog;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

import com.monas.client.util.Node;
import com.monas.client.util.UIHelper;

public class EditNodeDialog extends AbstractNodeDialog {

	private static final String MESSAGE = "Please completed node information";

	public EditNodeDialog(Shell parent, Node node) {
		super(parent, "Edit Node Form");
		super.node = node;
	}

	@Override
	protected Control createContents(Composite parent) {
		return createNewContents(parent, MESSAGE);
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayoutData(UIHelper.fillBoth());
		composite.setLayout(new GridLayout(1, false));

		nodeText = createText(composite, "Node Name");
		nodeText.setText(node.getNode());
		nodeText.setEnabled(false);

		hostText = createText(composite, "Host/IP Address");
		hostText.setText(node.getHost());
		hostText.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				node.setHost(hostText.getText());
				checkCompleted();
			}
		});

		osText = createText(composite, "Operating System");
		osText.setText(node.getOs());
		osText.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				node.setOs(osText.getText());
				checkCompleted();
			}
		});

		usernameText = createText(composite, "Username");
		usernameText.setText(node.getUsername());
		usernameText.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				node.setUsername(usernameText.getText());
				checkCompleted();
			}
		});

		passwordText = createText(composite, "Password");
		passwordText.setText(node.getPassword());
		passwordText.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				node.setPassword(passwordText.getText());
				checkCompleted();
			}
		});

		endText = createText(composite, "End Key");
		endText.setText(node.getEndLine());
		endText.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				node.setEndLine(endText.getText());
				checkCompleted();
			}
		});

		return composite;
	}
}
